<template>
  <div class="attrs-info">
    <!-- title弹窗标题 -->
    <!-- visible是否显示 -->
    <el-dialog :title="info.title" :visible.sync="info.isShow" @close="close">
      <!-- model:表单的数据 -->
      <!-- rules：检验的规则 -->
      <el-form :model="formData" label-width="100px" ref="formData">
        <el-form-item label="一级分类" prop="first_cateid">
          <el-select
            v-model="formData.first_cateid"
            placeholder="请选一级分类"
            @change="firstChange"
          >
            <el-option label="请选择" :value="0"></el-option>
            <el-option
              v-for="(item, index) in firstArr"
              :key="index"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" prop="second_cateid">
          <el-select
            v-model="formData.second_cateid"
            placeholder="请选择二级分类"
          >
            <el-option label="请选择" :value="0"></el-option>
            <el-option
              v-for="(item, index) in secondArr"
              :key="index"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品规格" prop="specsid">
          <el-select
            v-model="formData.specsid"
            placeholder="请选择商品规格"
            @change="specsChange"
          >
            <el-option label="请选择" :value="0"></el-option>
            <el-option
              v-for="(item, index) in specsArr"
              :key="index"
              :label="item.specsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性值" prop="specsattr">
          <el-select
            v-model="formData.specsattr"
            placeholder="请选择商品规格属性值"
            multiple 
          >
            <el-option label="请选择" :value="0"></el-option>
            <el-option
              v-for="(item, index) in specsAttrArr"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  specsAdd,
  specsEdit,
  getCatelist,
  getCateInfo,
  getSpecsList,
  getSpecsInfo
} from "../../api";
export default {
  props: ["info"],
  data() {
    return {
      // 表单的数据
      formData: {},
      // 一级分类对应的数组
      firstArr: [],
      // 二级分类对应的数组
      secondArr: [],
      // 商品规格对应的数组
      specsArr: [],
      // 商品规格属性值对应的数组
      specsAttrArr: []
    };
  },
  created() {
    // 获取一级分类对应的下拉框数据
    this.getFirstArr();
    // 获取商品规格的下拉框数据
    this.getSpecsArr();
  },
  methods: {
    setValue(data) {
      // 设置弹窗中的数据
      this.formData = { ...data };
    },
    // 获取一级分类对应的下拉框数据
    async getFirstArr() {
      let res = await getCatelist();
      this.firstArr = res.list;
    },
    // 监听一级分类的变化
    async firstChange() {
      let res = await getCateInfo({ pid: this.formData.first_cateid });
      this.secondArr = res.list;
    },
    // 获取商品规格的下拉框数据
    async getSpecsArr() {
      let res = await getSpecsList({ size: 99, page: 1 });
      this.specsArr = res.list;
    },
    // 监听商品规格的变化
    async specsChange() {
      let res = await getSpecsInfo({ id: this.formData.specsid });
      this.specsAttrArr = res.list[0].attrs
    },
    // 点击确认按钮，提交数据，关闭弹窗
    submitForm() {
      // this.$refs选择节点
      // validate() 是element ui的自带的检验方法
      this.$refs.formData.validate(async (valid) => {
        let res;
        if (valid) {
          // 判断新增还是修改
          if (this.formData.id) {
            // 有id说明是修改
            res = await specsEdit(this.formData);
          } else {
            // 新增
            res = await specsAdd(this.formData);
          }
          if (res.code === 200) {
            // 操作成功
            this.$message({
              message: res.msg,
              type: "success",
            });
            // 关闭弹窗
            this.$emit("close");
            this.$emit("update");
          }
        } else {
          console.log("err");
          return false;
        }
      });
    },
    // 关闭弹窗
    cancel() {
      // 子传父，发射自定义事件，通知父组件关闭弹窗
      this.$emit("close");
    },
    // ui框架关闭弹窗
    close() {
      // 关闭弹窗时清空数组，而且清空勾选的权限。避免数据影响
      this.formData.menus = [];
    },
  },
};
</script>

<style>
.attrs-info .attrs {
  width: 270px;
  margin-right: 20px;
  margin-bottom: 10px;
}
</style>